﻿' LibWowHeroes
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowHeroes

    ''' <summary>
    ''' A class that gets guild information from WoW Heroes
    ''' </summary>
    ''' <remarks></remarks>
    Public Class XmlGuild
        Inherits WowHeroesData

#Region "Declarations"

        Private xgWowHeroes As New XmlGuildSchema.wowheroes
        Private strZone As String
        Private strRealm As String
        Private strGuild As String
        Private cCharacters As List(Of Character)

#End Region

#Region "Properties"

        ''' <summary>
        ''' The zone the guild is in
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Zone() As String
            Get
                Return strZone
            End Get
            Set(ByVal value As String)
                strZone = value
            End Set
        End Property

        ''' <summary>
        ''' The realm the guild is on
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Realm() As String
            Get
                Return strRealm
            End Get
            Set(ByVal value As String)
                strRealm = value
            End Set
        End Property

        ''' <summary>
        ''' The name of the guild
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Guild() As String
            Get
                Return strGuild
            End Get
            Set(ByVal value As String)
                strGuild = value
            End Set
        End Property

        ''' <summary>
        ''' The URL that will be used to retrieve the guild from WoW Heroes
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL() As String
            Get
                Return String.Format("http://xml.wow-heroes.com/xml-guild.php?z={0}&r={1}&g={2}", strZone, strRealm, strGuild)
            End Get
        End Property

        ''' <summary>
        ''' WoW Heroes information for the guild's characters
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Characters() As List(Of Character)
            Get
                Return cCharacters
            End Get
        End Property

#End Region

#Region "Public Methods"

        ''' <summary>
        ''' A constructor to load a guild by zone, realm, and character name
        ''' </summary>
        ''' <param name="strZone"></param>
        ''' <param name="strRealm"></param>
        ''' <param name="strGuild"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strZone As String, ByVal strRealm As String, ByVal strGuild As String)
            Me.strZone = strZone
            Me.strRealm = strRealm
            Me.strGuild = strGuild
            Load()
        End Sub

        ''' <summary>
        ''' Loads the guild from WoW Heroes
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Load()
            Dim strKey As String = String.Format("LibWowHeroes.XmlGuild.{0}.{1}.{2}", strZone, strRealm, strGuild)
            If Cache.Cache(strKey) Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(xgWowHeroes.GetType())

                Dim srXML As New StringReader(MyBase.strData)

                xgWowHeroes = CType(xsSerializer.Deserialize(srXML), XmlGuildSchema.wowheroes)

                Cache.Cache.Insert(strKey, xgWowHeroes, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.Normal, Nothing)
            Else
                xgWowHeroes = CType(Cache.Cache(strKey), XmlGuildSchema.wowheroes)
            End If

            If xgWowHeroes Is Nothing Then
                Throw New InvalidOperationException("Character not found.")
            End If

            If xgWowHeroes.guild Is Nothing Then
                Throw New InvalidOperationException("Character not found.")
            End If

            If xgWowHeroes.guild.Count = 0 Then
                Throw New InvalidOperationException("Character not found.")
            End If

            If xgWowHeroes.guild.First().character Is Nothing Then
                Throw New InvalidOperationException("Character not found.")
            End If

            cCharacters = New List(Of Character)
            For Each cCharacter As XmlGuildSchema.character In xgWowHeroes.guild.First().character
                cCharacters.Add(New Character(cCharacter.name, CType(cCharacter.raceId, RaceType), CType(cCharacter.genderId, GenderType), CType(cCharacter.classId, ClassType), cCharacter.level, cCharacter.talents, cCharacter.specName, cCharacter.guildRank, cCharacter.score, CDate(cCharacter.date), cCharacter.suggest))
            Next
        End Sub

#End Region

#Region "Nested Classes"

        ''' <summary>
        ''' A class that encapsulates character information
        ''' </summary>
        ''' <remarks></remarks>
        Public Class Character

            Private strName As String
            Private rRace As RaceType
            Private gGender As GenderType
            Private cClass As ClassType
            Private intLevel As Integer
            Private strTalents As String
            Private strSpec As String
            Private intGuildRank As Integer
            Private intScore As Integer
            Private dtDate As Date
            Private strSuggest As String

            ''' <summary>
            ''' The name of the character
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Name() As String
                Get
                    Return strName
                End Get
                Set(ByVal value As String)
                    strName = value
                End Set
            End Property

            ''' <summary>
            ''' The character's race
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Race() As RaceType
                Get
                    Return rRace
                End Get
                Set(ByVal value As RaceType)
                    rRace = value
                End Set
            End Property

            ''' <summary>
            ''' The character's gender
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Gender() As GenderType
                Get
                    Return gGender
                End Get
                Set(ByVal value As GenderType)
                    gGender = value
                End Set
            End Property

            ''' <summary>
            ''' The class of the character
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property [Class]() As ClassType
                Get
                    Return cClass
                End Get
                Set(ByVal value As ClassType)
                    cClass = value
                End Set
            End Property

            ''' <summary>
            ''' The character's level
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Level() As Integer
                Get
                    Return intLevel
                End Get
                Set(ByVal value As Integer)
                    intLevel = value
                End Set
            End Property

            ''' <summary>
            ''' The character's talent spec
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Talents() As String
                Get
                    Return strTalents
                End Get
                Set(ByVal value As String)
                    strTalents = value
                End Set
            End Property

            ''' <summary>
            ''' The character's spec
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Spec() As String
                Get
                    Return strSpec
                End Get
                Set(ByVal value As String)
                    strSpec = value
                End Set
            End Property

            ''' <summary>
            ''' The character's guild rank
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property GuildRank() As Integer
                Get
                    Return intGuildRank
                End Get
                Set(ByVal value As Integer)
                    intGuildRank = value
                End Set
            End Property

            ''' <summary>
            ''' The character's WoW Heroes score
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Score() As Integer
                Get
                    Return intScore
                End Get
                Set(ByVal value As Integer)
                    intScore = value
                End Set
            End Property

            ''' <summary>
            ''' The date the character was cached on WoW Heroes
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property [Date]() As Date
                Get
                    Return dtDate
                End Get
                Set(ByVal value As Date)
                    dtDate = value
                End Set
            End Property

            ''' <summary>
            ''' Suggested instances that this character should run
            ''' </summary>
            ''' <value></value>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Property Suggest() As String
                Get
                    Return strSuggest
                End Get
                Set(ByVal value As String)
                    strSuggest = value
                End Set
            End Property

            ''' <summary>
            ''' A constructor to create new character information
            ''' </summary>
            ''' <param name="strName"></param>
            ''' <param name="rRace"></param>
            ''' <param name="gGender"></param>
            ''' <param name="cClass"></param>
            ''' <param name="intLevel"></param>
            ''' <param name="strTalents"></param>
            ''' <param name="strSpec"></param>
            ''' <param name="intGuildRank"></param>
            ''' <param name="intScore"></param>
            ''' <param name="dtDate"></param>
            ''' <param name="strSuggest"></param>
            ''' <remarks></remarks>
            Public Sub New(ByVal strName As String, ByVal rRace As RaceType, ByVal gGender As GenderType, ByVal cClass As ClassType, ByVal intLevel As Integer, ByVal strTalents As String, ByVal strSpec As String, ByVal intGuildRank As Integer, ByVal intScore As Integer, ByVal dtDate As Date, ByVal strSuggest As String)
                Me.strName = strName
                Me.rRace = rRace
                Me.gGender = gGender
                Me.cClass = cClass
                Me.intLevel = intLevel
                Me.strTalents = strTalents
                Me.strSpec = strSpec
                Me.intGuildRank = intGuildRank
                Me.intScore = intScore
                Me.dtDate = dtDate
                Me.strSuggest = strSuggest
            End Sub

        End Class

#End Region

    End Class

End Namespace